import { Vue, Component, Prop, Provide, Emit, Watch } from 'vue-property-decorator';
import { Subject } from 'rxjs';
import { CreateElement } from 'vue';
import './bxdworkflow-design.less';

import HtmlViewEngine from '@engine/view/html-view-engine';


@Component({
    components: {
    },
    beforeRouteEnter: (to: any, from: any, next: any) => {
        next((vm: any) => {
            vm.$store.commit('addCurPageViewtag', { fullPath: to.fullPath, viewtag: vm.viewtag });
        });
    },
})
export default class BXDWorkflowDesign extends Vue {

    /**
     * 数据变化
     *
     * @param {*} val
     * @returns {*}
     * @memberof BXDWorkflowDesign
     */
    @Emit() 
    public viewDatasChange(val: any):any {
        return val;
    }

    /**
     * 数据视图
     *
     * @type {string}
     * @memberof BXDWorkflowDesign
     */
    @Prop() public viewdata!: string;

	/**
	 * 视图标识
	 *
	 * @type {string}
	 * @memberof AppDashboardView
	 */
	public viewtag: string = '7efc42122096e92e77c14d86ddb71162';

    /**
     * 父数据对象
     *
     * @protected
     * @type {*}
     * @memberof BXDWorkflowDesign
     */
    protected srfparentdata: any = {};

    /**
     * 视图模型数据
     *
     * @type {*}
     * @memberof BXDWorkflowDesign
     */
    public model: any = {
        srfTitle: '流程设计',
        srfCaption: 'bxd.views.workflowdesign.caption',
        srfSubCaption: '',
        dataInfo: ''
    }

    /**
     * 处理值变化
     *
     * @param {*} newVal
     * @param {*} oldVal
     * @memberof BXDWorkflowDesign
     */
    @Watch('viewdata')
    onViewData(newVal: any, oldVal: any) {
        const _this: any = this;
        if (!Object.is(newVal, oldVal) && _this.engine) {
            _this.engine.setViewData(newVal);
            _this.engine.load();
        }
    }

    /**
     * 容器模型
     *
     * @type {*}
     * @memberof BXDWorkflowDesign
     */
    public containerModel: any = {
        wflinks: [],
    };

    /**
     * 视图状态订阅对象
     *
     * @private
     * @type {Subject<{action: string, data: any}>}
     * @memberof BXDWorkflowDesign
     */
    public viewState: Subject<ViewState> = new Subject();


    /**
     * 视图引擎
     *
     * @private
     * @type {Engine}
     * @memberof BXDWorkflowDesign
     */
    private engine: HtmlViewEngine = new HtmlViewEngine();

    /**
     * 引擎初始化
     *
     * @private
     * @memberof BXDWorkflowDesign
     */
    private engineInit(): void {
        this.engine.init({
            view: this,
            isLoadDefault: true,
        });
    }

    /**
     * Vue声明周期
     *
     * @memberof BXDWorkflowDesign
     */
    public created() {
        const secondtag = this.$util.createUUID();
        this.$store.commit('viewaction/createdView', { viewtag: this.viewtag, secondtag: secondtag });
        this.viewtag = secondtag;
        
    }

    /**
     * 销毁之前
     *
     * @memberof BXDWorkflowDesign
     */
    public beforeDestroy() {
        this.$store.commit('viewaction/removeView', this.viewtag);
    }

    /**
     * Vue声明周期(组件初始化完毕)
     *
     * @memberof BXDWorkflowDesign
     */
    public mounted() {
        const _this: any = this;
        _this.engineInit();
        if (_this.loadModel && _this.loadModel instanceof Function) {
            _this.loadModel();
        }
        
    }




    /**
     * 关闭视图
     *
     * @param {any[]} args
     * @memberof BXDWorkflowDesign
     */
    public closeView(args: any[]): void {
        let _view: any = this;
        if (_view.viewdata) {
            _view.$emit('viewdataschange', args);
            _view.$emit('close');
        } else if (_view.$tabPageExp) {
            _view.$tabPageExp.onClose(_view.$route.fullPath);
        }
    }

    /**
     * 视图 viewurl
     *
     * @type {string}
     * @memberof BXDWorkflowDesign
     */
    public viewurl: string = '';

    /**
     * 序列号
     *
     * @private
     * @type {number}
     * @memberof BXDWorkflowDesign
     */
    private serialNumber: number = this.$util.createSerialNumber();

    /**
     * 请求行为序列号数组
     *
     * @private
     * @type {any[]}
     * @memberof BXDWorkflowDesign
     */
    private serialsNumber: any[] = [];

    /**
     * 添加序列号
     *
     * @private
     * @param {*} action
     * @param {number} serialnumber
     * @memberof BXDWorkflowDesign
     */
    private addSerialNumber(action: any, serialnumber: number): void {
        const index = this.serialsNumber.findIndex((serial: any) => Object.is(serial.action, action));
        if (index === -1) {
            this.serialsNumber.push({ action: action, serialnumber: serialnumber })
        } else {
            this.serialsNumber[index].serialnumber = serialnumber;
        }
    }

    /**
     * 删除序列号
     *
     * @private
     * @param {*} action
     * @returns {number}
     * @memberof BXDWorkflowDesign
     */
    private getSerialNumber(action: any): number {
        const index = this.serialsNumber.findIndex((serial: any) => Object.is(serial.action, action));
        return this.serialsNumber[index].serialnumber;
    }

    /**
     * 加载模型
     *
     * @memberof BXDWorkflowDesign
     */
    public loadModel(): void {
        const _url: string = '/webapp/bx/bxd/workflowdesign/getmodel';
        const _data: any = {};
        if (this.engine && this.engine.viewdata) {
            const _viewdata = this.engine.viewdata;
            if (_viewdata.srfkey) {
                Object.assign(_data, { srfkey: _viewdata.srfkey });
            } else if (_viewdata.srfparentdata && _viewdata.srfparentdata.srfparentkey) {
                Object.assign(_data, { srfkey: _viewdata.srfparentdata.srfparentkey });
            }
        }

        this.serialNumber++;
        const serialnumber = this.serialNumber;
        this.addSerialNumber('getmodel', serialnumber);

        const get: Promise<any> = this.$http.get(_url, _data, false, serialnumber);
        get.then((response: any) => {
            const { serialnumber: _serialnumber } = response;
            const lastserialnumber = this.getSerialNumber('getmodel');
            if (_serialnumber && lastserialnumber && _serialnumber < lastserialnumber) {
                return;
            }

            if (!response || response.status !== 200) {
                this.$Notice.error({ title: '加载失败', desc: response.info });
            }
            const { data: _data } = response;
            if (_data.viewurl) {
                this.viewurl = _data.viewurl;
            }
        }).catch((response: any) => {
            const { serialnumber: _serialnumber } = response;
            const lastserialnumber = this.getSerialNumber('getmodel');
            if (_serialnumber && lastserialnumber && _serialnumber < lastserialnumber) {
                return;
            }

            const { data: _data } = response;
            this.$Notice.error({ title: '加载失败', desc: _data.message });
        });
    }


    /**
     * 绘制视图消息 （上方）
     *
     * @returns
     * @memberof BXDWorkflowDesign
     */
    public renderPosTopMsgs() {
        return (
            <div class='view-top-messages'>
            </div>
        );
    }

    /**
     * 绘制视图消息 （下方）
     *
     * @returns
     * @memberof BXDWorkflowDesign
     */
    public renderPosBottomMsgs() {
        return (
            <div class='view-bottom-messages'>
            </div>
        );
    }
    
    /**
     * 绘制内容
     *
     * @param {CreateElement} h
     * @returns
     * @memberof BXDWorkflowDesign
     */
    public render(h: CreateElement) {
        return (
        <div class="view-container bxdworkflow-design">
            <card class='view-card view-no-caption' dis-hover={true} padding={0} bordered={false}>
        
                
                <div class="content-container">
                    <iframe src={this.viewurl} frameborder="0" height="100%" width="100%"></iframe>
                </div>
            </card>
        </div>
        );
    }

}